package com.leetcode.stackandqueue.demo3;

import java.util.Stack;

/**
 * @Author Zan
 * @Create 2024/7/2 20:12
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/valid-parentheses/description/ 有效的括号
 */
public class Test {
    public static void main(String[] args) {
        Solution solution = new Solution();
        boolean valid = solution.isValid("((");
        System.out.println(valid);
    }
}

class Solution {
    public boolean isValid(String s) {
        // 使用栈，每次取出两个，进行比较
        if (s.length() % 2 == 1) {
            // 说明是奇数
            return false;
        }
        // 如果说是左符号，则存入对应的右符号，如果是右符号，则进行取出
        Stack<Character> stack1 = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(') {
                stack1.push(')');
            } else if (c == '[') {
                stack1.push(']');
            } else if (c == '{') {
                stack1.push('}');
            } else if (c == ')' || c == ']' || c == '}') {
                if (stack1.isEmpty()) {
                    return false;
                }
                Character pop = stack1.pop();
                if (!pop.equals(c)) {
                    return false;
                }
            }
        }
        return stack1.isEmpty();
    }
}